package com.hzdesk.media.koreabatchmatvector.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.inter.OnOptionItemClickListener;

import java.util.List;

/**
 * Created by Administrator on 2018/7/20.
 * 鼠标笔触大小适配器
 */

public class MouseStrokesAdapter extends RecyclerView.Adapter{
    private Context context;
    private List<Integer> mList;
    private int SelectPosition=-1;
    private OnOptionItemClickListener onOptionItemClickListener;

    public void setOnOptionItemClickListener(OnOptionItemClickListener onOptionItemClickListener) {
        this.onOptionItemClickListener = onOptionItemClickListener;
    }

    public MouseStrokesAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.mouse_strokes_item,parent,false);
        MyMouseStrokesViewhoder myMouseStrokesViewhoder=new MyMouseStrokesViewhoder(view);
        return myMouseStrokesViewhoder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof  MyMouseStrokesViewhoder){
            final int finalposition=position;
            MyMouseStrokesViewhoder myMouseStrokesViewhoder= (MyMouseStrokesViewhoder) holder;
            myMouseStrokesViewhoder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onOptionItemClickListener!=null){
                        onOptionItemClickListener.OnItemClick(finalposition);
                    }

                }
            });
            if (SelectPosition!=-1&&SelectPosition==position){
                myMouseStrokesViewhoder.textView_current.setVisibility(View.VISIBLE);
            }else {
                myMouseStrokesViewhoder.textView_current.setVisibility(View.INVISIBLE);
            }
            ViewGroup.LayoutParams layoutParams=myMouseStrokesViewhoder.textView.getLayoutParams();
            layoutParams.height=mList.get(position);
            myMouseStrokesViewhoder.textView.setLayoutParams(layoutParams);
            myMouseStrokesViewhoder.linearLayout.setOnHoverListener(onHoverListener);
            myMouseStrokesViewhoder.linearLayout.setOnFocusChangeListener(onFocusChangeListener);

        }


    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    public void addItemDate(List<Integer> list) {
        mList = list;
        notifyDataSetChanged();
    }
    public void setSelectPostion(int position){
        SelectPosition=position;
    }

    private class MyMouseStrokesViewhoder extends RecyclerView.ViewHolder{
        TextView textView;
        TextView textView_current;
        LinearLayout linearLayout;

        public MyMouseStrokesViewhoder(View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.mouse_strokes_item_textview);
            textView_current=itemView.findViewById(R.id.mouse_strokes_item_textview_current);
            linearLayout=itemView.findViewById(R.id.mouse_strokes_item_lin);
        }
    }
    private View.OnHoverListener onHoverListener = new View.OnHoverListener() {
        @Override
        public boolean onHover(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_HOVER_ENTER:
                    v.requestFocus();
                    v.requestFocusFromTouch();
                    break;
                case MotionEvent.ACTION_HOVER_EXIT:
                    break;
                case MotionEvent.ACTION_HOVER_MOVE:
                    break;
            }
            return false;
        }
    };

    private View.OnFocusChangeListener onFocusChangeListener=new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            LinearLayout linearLayout= (LinearLayout) v;
            if (hasFocus){
                linearLayout.setBackgroundResource(R.drawable.selector_view_shape);
                ObjectAnimator scaleX=ObjectAnimator.ofFloat(v,"scaleX",1.0f,1.02f);
                ObjectAnimator scaleY=ObjectAnimator.ofFloat(v,"scaleY",1.0f,1.02f);
                ObjectAnimator translationZ=ObjectAnimator.ofFloat(v,"translationZ",0f,2.0f);
                AnimatorSet animatorSet=new AnimatorSet();
                animatorSet.play(scaleX).with(scaleY).with(translationZ);
                animatorSet.setDuration(200);
                animatorSet.start();
            }else {
                linearLayout.setBackgroundResource(R.drawable.view_shape);
                ObjectAnimator scaleX_01=ObjectAnimator.ofFloat(v,"scaleX",1.02f,1.0f);
                ObjectAnimator scaleY_01=ObjectAnimator.ofFloat(v,"scaleY",1.02f,1.0f);
                ObjectAnimator translationZ_01=ObjectAnimator.ofFloat(v,"translationZ",2.0f,0f);
                AnimatorSet animatorSet_01=new AnimatorSet();
                animatorSet_01.setDuration(200);
                animatorSet_01.play(scaleX_01).with(scaleY_01).with(translationZ_01);
                animatorSet_01.start();
            }

        }
    };

}
